<%@page import="com.hrms.core.Constants"%>
<%@page import="java.util.Arrays"%>
<%@page import="com.google.appengine.api.datastore.KeyFactory"%>
<%@page import="com.hrms.core.RoleManager"%>
<%@page import="org.apache.commons.lang.StringUtils"%>
<%@page import="com.hrms.domain.BusinessObjectImpl"%>
<%@page import="com.hrms.domain.Employee"%>
<%@page import="com.hrms.core.Utils"%>
<%@ taglib prefix="c" tagdir="/WEB-INF/tags" %>
<html><head>
<script type="text/javascript"><!--
var stringFields = new Array("initials", "name", "supervisor", "team");
var dateFields = new Array("joinedDate");
var intFields = new Array();
var timeFields = new Array();
var emailFields = new Array();
var doubleFields = new Array();

<%

if(request.getParameter(Constants.ACTION) != null){
	Employee serviceEmployee =  new Employee(request.getParameterMap());	
	String warning = serviceEmployee.fireEvents(request.getParameter(Constants.ACTION).trim(), request);
	
	if(StringUtils.length(warning) > 0){
		request.setAttribute("msg", warning);
	}
}

boolean isEdit =(request.getParameter(Constants.SELECTED_EMPLOYEE) != null);
Employee emp=null;
if (isEdit) {
	emp = BusinessObjectImpl.selectAsSingleResult(Employee.class, "where userId==:p0" , new Object[]{ request.getParameter("selectEmp").trim() } );
}else{
	out.print("emailFields.push('userId');");
}

String userId = isEdit ? emp.getUserId() : StringUtils.EMPTY;
String empNo = isEdit ? emp.getEmpNo() : StringUtils.EMPTY;
String name = isEdit ? emp.getName() : StringUtils.EMPTY;
String initials = isEdit ? emp.getInitials() : StringUtils.EMPTY;

String joinedDate = isEdit && (emp.getJoinedDate()!=null) ? Utils.formatToSimpleDate(emp.getJoinedDate()) : StringUtils.EMPTY;

String role = isEdit ? Utils.formatString(emp.getRole().getName()) : Constants.EMPTY_STRING;
role=Utils.makeSelect("role", RoleManager.ROLE.values(),role);


String team = isEdit ? Utils.formatString(emp.getTeam()) : StringUtils.EMPTY;
team = Utils.makeSelect("team", Constants.TEAM.values() ,team);

String supervisor = isEdit ? Utils.formatString(emp.getSupervisor()) : Constants.EMPTY_STRING;
supervisor = Employee.makeSelect("supervisor", supervisor);

String employmentType  = isEdit ? Utils.formatString(emp.getEmploymentType()) : Constants.EMPTY_STRING;
employmentType=Utils.makeSelect("employmentType", Employee.EmploymentType.values(),employmentType);

String endOfProbation = isEdit && (emp.getEndOfProbation()!=null) ? Utils.formatToSimpleDate(emp.getEndOfProbation()) : StringUtils.EMPTY;

%>
--></script>
</head>
<br/>
<body>
<%@include file="/WEB-INF/include/msg.jsp" %>
<form id="serviceForm" name="serviceForm" action="manageEmployee.jsp" method="post">
<%@include file="/WEB-INF/include/userAction.jsp" %>
<%@include file="/WEB-INF/include/actionEvent.jsp" %>
  <table width="100%" align="left" cellpadding="0" border="0" cellspacing="0">
      <tr> 
        <td> <table width="100%" cellpadding="4" border="0" 
				cellspacing="1" class="listtablebordercolor">
            <tr> 
              <td colspan="2" class="datafield ui-tabs-nav ui-helper-reset ui-helper-clearfix ui-widget-header ui-corner-all">
              Employee Record</td>
            </tr>
            
            <%if (isEdit) { %>
					<c:tableRow label="Id" name="key" type="hidden" value="<%=KeyFactory.keyToString( emp.getKey() )%>" input="<%= String.valueOf( emp.getKey().getId()) %>" />
					<c:tableRow label="Created By" name="createdByUser" type="hidden" value="<%= Utils.formatString(emp.getCreatedByUser())%>" input="<%= Utils.formatString(emp.getCreatedByUser())%>" />
					<c:tableRow label="Last Modfied Date" name="LastModfiedDate" input="<%=Utils.formatDate(emp.getCreateTimestamp())%>"  />
					<c:tableRow label="Last Modified By" name="lastModifyUser" input="<%= Utils.formatString( emp.getLastModifyUser())%>" />
					<c:tableRow label="User Email Address" name="userId" input="<%= userId %>"  type="hidden" value="<%= userId %>"/>
			<%}else{ %>			
			<c:tableRow label="User Email Address" name="userId" type="text" value="<%= userId %>"  maxlen="500" size="60" ></c:tableRow>
			<%} %>
            <c:tableRow label="Employee No" name="empNo" type="text" value="<%= Utils.escapeHTML( empNo )%>"  maxlen="10" ></c:tableRow> 
            <c:tableRow label="Initials" name="initials" type="text" value="<%= Utils.escapeHTML( initials )%>" maxlen="3" size="4"></c:tableRow>
            <c:tableRow label="Name" name="name" type="text" value="<%= Utils.escapeHTML( name )%>" maxlen="500" size="90"></c:tableRow>
            <c:tableRow label="Employment Type" name="employmentType" input="<%= employmentType %>" maxlen="500"></c:tableRow>
            <c:tableRow label="End Of Probation" name="endOfProbation" type="text" maxlen="10" datefield="yes" value="<%= endOfProbation %>"  />
            <c:tableRow label="Date of Joining" name="joinedDate" type="text" maxlen="10" datefield="yes" value="<%= joinedDate %>"  />
            <c:tableRow label="Team" name="team"  input="<%= team %>" ></c:tableRow>
            <c:tableRow label="Supervisor" name="supervisor"  input="<%= supervisor %>" ></c:tableRow>
            <c:tableRow label="Role" name="role" input="<%= role %>" maxlen="500"></c:tableRow>
            
          </table></td>
        <td rowspan="2" width="3" valign="TOP" class="listtableshadowcolor"><img
				src="/static/images/fourpixel.gif" width="3" height="15" border="0"></td>
        <td width="10%" rowspan="2"><img src="/static/images/apixel.gif"
				width="1" height="1" border="0"></td>
      </tr>
      <tr> 
        <td class="listtableshadowcolor"><img
				src="/static/images/fourpixel.gif" width="16" height="4" border="0"></td>
      </tr>
      <tr> 
        <td align="right"><input type="button" value="Save"
				onclick="submitForm(this)" class="ui-state-default ui-corner-all ui-state-hover"></td>
      </tr>
  </table>
</form>
<c:focus focusId="empNo"/>
</body>
</html>